import java.util.Arrays;
import java.util.Scanner;

public class Exercise_OddBeforeEven {


    public static void func1(int[] arr) {
        int left=0;
        int right=arr.length-1;
        while(left<right) {
            while(left<right && arr[left]%2!=0) {
                left++;
            }
            while(left<right && arr[right]%2==0) {
                right--;
            }
            int tmp=arr[left];
            arr[left]=arr[right];
            arr[right]=tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        func1(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void main1(String[] args) {
        //调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入数组长度:");
        int n=sc.nextInt();
        int[] arr=new int[n];
        System.out.print("请输入这"+n+"个数：");
        for(int i=0;i<n;i++) {
            arr[i]=sc.nextInt();
        }

        int[] arrOdd=new int[n];
        int[] arrEven=new int[n];
        int retOdd=-1;
        int retEven=-1;

        for(int i=0;i<n;i++) {
            if(arr[i]%2==0) {
                arrEven[++retEven]=arr[i];
            }
            else arrOdd[++retOdd]=arr[i];
        }

        //arraycopy
        int[] arr2=new int[n];
        System.arraycopy(arrOdd,0,arr2,0,retOdd+1);
        System.arraycopy(arrEven,0,arr2,retOdd+1,retEven+1);
        System.out.print("这个数组经过排列（将奇数排在偶数前面）后：");
        for(int i=0;i<arr2.length;i++) {
            System.out.print(arr2[i]+" ");
        }
        System.out.println();
    }
}
